<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="申请时间">
        <el-date-picker
          v-model="daterangeTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="关联订单" prop="orderGl">
        <el-input
          v-model="queryParams.orderGl"
          placeholder="请输入关联订单"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属用户" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入所属用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="售后类型" prop="saleType">
        <el-select
          v-model="queryParams.saleType"
          placeholder="请选择售后类型"
          clearable
          @change="handleQuery"
        >
          <!-- 下拉选项列表，可根据实际业务场景修改选项值 -->
          <el-option
            v-for="(item, index) in saleTypeOptions"
            :key="index"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>



      <el-form-item label="售后原因" prop="saleReason">
        <el-select
          v-model="queryParams.saleReason"
          placeholder="请选择售后原因"
          clearable
          @change="handleQuery"
        >
          <!-- 下拉选项列表，可根据实际业务场景修改选项值 -->
          <el-option
            v-for="(item, index) in saleReasonOption"
            :key="index"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>



      <el-form-item label="驿站名称" prop="shopName">
        <el-select
          v-model="queryParams.shopName"
          placeholder="请选择驿站名称"
          clearable
          @change="handleQuery"
        >
          <!-- 下拉选项列表，可根据实际业务场景修改选项值 -->
          <el-option
            v-for="(item, index) in shopNameOption"
            :key="index"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>




      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sale:sale:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sale:sale:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sale:sale:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sale:sale:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="saleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="售后id" align="center" prop="saleId" />
      <el-table-column label="申请时间" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="关联订单" align="center" prop="orderGl" />
      <el-table-column label="所属用户" align="center" prop="username" />
      <el-table-column label="驿站名称" align="center" prop="shopName" />
      <el-table-column label="退款金额" align="center" prop="refundPrice" />
      <el-table-column label="售后类型" align="center" prop="saleType" />
      <el-table-column label="售后原因" align="center" prop="saleReason" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sale:sale:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sale:sale:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改售后管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="申请时间" prop="time">
          <el-date-picker clearable
            v-model="form.time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="关联订单" prop="orderGl">
          <el-input v-model.number="form.orderGl" placeholder="请输入关联订单" type="number" />
        </el-form-item>
        <el-form-item label="所属用户" prop="username">
          <el-input v-model="form.username" placeholder="请输入所属用户" />
        </el-form-item>


        <el-form-item label="驿站名称" prop="shopName">
          <el-select v-model="form.shopName" placeholder="请选择驿站名称">
            <el-option label="京东代取点" value="京东代取点" />
            <el-option label="菜鸟驿站" value="菜鸟驿站" />
            <el-option label="韵达代取点" value="韵达代取点" />
            <el-option label="圆通代取点" value="圆通代取点" />
            <el-option label="顺丰代取点" value="顺丰代取点" />
            <el-option label="邮政代取点" value="邮政代取点" />
          </el-select>
        </el-form-item>

        <el-form-item label="售后类型" prop="saleType">
          <el-select v-model="form.saleType">
            <el-option label="少件" value="少件" />
            <el-option label="漏件" value="漏件" />
            <el-option label="仅退款" value="仅退款" />
            <el-option label="未收到货" value="未收到货" />
          </el-select>
        </el-form-item>

        <el-form-item label="退款金额" prop="refundPrice">
          <el-input v-model.number="form.refundPrice" placeholder="请输入退款金额" type="number" />
        </el-form-item>



        <el-form-item label="售后原因" prop="saleReason">
          <el-select v-model="form.saleReason">
            <el-option label="7天无理由" value="7天无理由" />
            <el-option label="不喜欢" value="不喜欢" />
            <el-option label="多拍了" value="多拍了" />
            <el-option label="拍错尺码" value="拍错尺码" />
            <el-option label="地址填错了" value="地址填错了" />
            <el-option label="商家长时间未发货" value="商家长时间未发货" />
          </el-select>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSale, getSale, delSale, addSale, updateSale } from "@/api/sale/sale"

export default {
  name: "Sale",
  data() {
    return {
      // 遮罩层
      loading: true,

      saleTypeOptions: [
        { label: '少件', value: '少件' },
        { label: '漏件', value: '漏件' },
        { label: '仅退款', value: '仅退款' },
        { label: '未收到货', value: '未收到货' }
      ],

      saleReasonOption: [
        { label: '7天无理由', value: '7天无理由' },
        { label: '不喜欢', value: '不喜欢' },
        { label: '多拍了', value: '多拍了' },
        { label: '拍错尺码', value: '拍错尺码' },
        { label: '地址填错了', value: '地址填错了' },
        { label: '商家长时间未发货', value: '商家长时间未发货' },
      ],

      shopNameOption: [
        { label: '菜鸟驿站', value: '菜鸟驿站' },
        { label: '韵达代取点', value: '韵达代取点' },
        { label: '京东代取点', value: '京东代取点' },
        { label: '圆通代取点', value: '圆通代取点' },
        { label: '顺丰代取点', value: '顺丰代取点' },
        { label: '邮政代取点', value: '邮政代取点' },
      ],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 售后管理表格数据
      saleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 订单状态时间范围
      daterangeTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        time: null,
        orderGl: null,
        username: null,
        shopName: null,
        saleType: null,
        saleReason: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询售后管理列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeTime && '' != this.daterangeTime) {
        this.queryParams.params["beginTime"] = this.daterangeTime[0]
        this.queryParams.params["endTime"] = this.daterangeTime[1]
      }
      listSale(this.queryParams).then(response => {
        this.saleList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        saleId: null,
        time: null,
        orderGl: null,
        username: null,
        shopName: null,
        refundPrice: null,
        saleType: null,
        saleReason: null,
        status: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeTime = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.saleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加售后管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const saleId = row.saleId || this.ids
      getSale(saleId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改售后管理"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.saleId != null) {
            updateSale(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addSale(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const saleIds = row.saleId || this.ids
      this.$modal.confirm('是否确认删除售后管理编号为"' + saleIds + '"的数据项？').then(function() {
        return delSale(saleIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sale/sale/export', {
        ...this.queryParams
      }, `sale_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
